


function(){
var req = [];
var MAXID = 999999999999999999999;

function TweetManager() {
    this.home_max_id = "";	// for home timeline next request
    this.since_id = ["", "", "", ""]; // for mentions, user, favorites; since_id[0] = 'home', not used
    this.home_read_minid = MAXID;     // for home unread
    this.mentions_read_minid = MAXID; // for mentions unread
    this.unread = [[], []];	      // unread[0] ~ home, unread[1] ~ mentions
    this.read = [[], []];
    this.favorites = [];	// favorites items
    this.inbox = [];
    this.sent = [];
    this.mine = [];
    this.user_screen_name = "";
    this.user_name = "";
    this.faved = [];
    this.deleted = [];
    this.deleted_dm = [];
    this.currentTabId = 0;
    /* compose data */
    this.isComposing = false;
    this.saveMessage = "";
    this.replyId = null;
    this.retweetId = null;
}

function getCurrentUserInfo() {
    var rnd = new Date().getTime();
    var url = "http://t.163.com/account/settings/list?" + "&rnd=" + rnd;
    var http = new XMLHttpRequest();
    http.open("GET",
	     url,
	     false);
    http.onload = function() {
	if (http.status != 200) {
	    console.log("get account info failed " + http.status + http.responseText);
	    return;
	}
	var res = eval('(' + http.responseText + ')');
	var instance = TweetManager.instance;
	if (res.screen_name != null)
	    instance.user_screen_name = res.screen_name;
	if (res.name != null)
	    instance.user_name = res.name;
	console.log("account info: " + instance.user_name + " " + instance.user_screen_name);
    }
    http.send(null);
}

function updateNewTwit(id) {
    if (id == 2 && TweetManager.instance.user_screen_name == "") {
	getCurrentUserInfo();
    }

    var names = ["statuses/home_timeline",
		 "statuses/mentions",
		 "statuses/user_timeline/" + TweetManager.instance.user_screen_name,
		 "favorites",
		 "direct_messages",
		 "direct_messages/sent"
		];

    var TBASE = "http://t.163.com/";
    var rnd = new Date().getTime();
    var url = TBASE + names[id] + ".json?";
    if (id == 0) {
	url = url +  "max_id=" + TweetManager.instance.home_max_id + "&rnd=" + rnd;
    } else {
	url = url + "since_id=" + TweetManager.instance.since_id[id] + "&rnd=" + rnd;
    }
    req[id] = new XMLHttpRequest();
    req[id].open("GET",
		 url,
		 true);
    switch (id) {
    case 0:
	req[id].onload = onNewHomeTweets;
	break;
    case 1:
	req[id].onload = onNewMentions;
	break;
    case 2:
	req[id].onload = onNewMine;
	break;
    case 3:
	req[id].onload = onNewFavorites;
	break;
    case 4:
	req[id].onload = onNewInbox;
	break;
    case 5:
	req[id].onload = onNewSent;
	break;
    }
    req[id].send(null);
}

function onNewHomeTweets() {
    if (req[0].status != 200) {
	console.log(req[0].status + " " + req[0].responseText);
	return;
    }
    var res = eval('(' + req[0].responseText + ')');
    if (res.length == 0) {
	return;
    } else {
	var instance = TweetManager.instance;
	if (res.length > 0) {
	    instance.home_max_id = res[0].id;
	    //from old to new, id1<id2 ==> id1 is newer than id2
	    for (var i = res.length - 1; i >= 0; i--) {
		if (res[i].id < instance.home_read_minid) {
		    instance.unread[0].unshift(res[i]);
		} else {
		    instance.read[0].unshift(res[i]);
		}
//		console.log(res[res.length - 1 - i].id);
	    }
	}
	if (instance.unread[0].length + instance.unread[1].length > 0)
	    chrome.browserAction.setBadgeText({text:String(instance.unread[0].length + instance.unread[1].length)});
    }
}

function onNewMentions() {
    if (req[1].status != 200) {
	console.log(req[1].status);
	return;
    }
    var res = eval('(' + req[1].responseText + ')');
    if (res.length == 0) {
	return;
    } else {
	var instance = TweetManager.instance;
	if (res.length > 0) {
	    //from old to new, id1<id2 ==> id1 is newer than id2
	    // new mentions may be repeated
	    var existmin = MAXID;
	    if (instance.read[1].length > 0) {
		existmin = instance.read[1][0].id;
	    }
	    if (instance.unread[1].length > 0) {
		existmin = instance.unread[1][0].id;
	    }
	    for (var i = res.length - 1; i >= 0; i--) {
		if (res[i].id < existmin) {
		    if (res[i].id < instance.mentions_read_minid) {
			instance.unread[1].unshift(res[i]);
		    } else {
			instance.read[1].unshift(res[i]);
		    }
		}
//    		console.log(res[res.length-1 - i].id, res[res.length-1 -i].text);
	    }
	}
	if (instance.unread[0].length + instance.unread[1].length > 0)
	    chrome.browserAction.setBadgeText({text:String(instance.unread[0].length + instance.unread[1].length)});
    }
}

function onNewMine() {
    if (req[2].status != 200) {
	console.log(req[2].status + " " + req[2].responseText);
	return;
    }
    var res = eval('(' + req[2].responseText + ')');
    if (res.length == 0) {
	return;
    } else {
	var instance = TweetManager.instance;
	if (res.length > 0) {
	    //from old to new, id1<id2 ==> id1 is newer than id2
	    // new mine may be repeated
	    var existmin = MAXID;
	    if (instance.mine.length > 0) {
		existmin = instance.mine[0].id;
	    }
	    for (var i = res.length - 1; i >= 0; i--) {
		if (res[i].id < existmin) {
		    instance.mine.unshift(res[i]);
		}
//    		console.log(res[res.length-1 - i].id, res[res.length-1 -i].text);
	    }
	}
    }
}

function onNewFavorites() {
    if (req[3].status != 200) {
	console.log(req[3].status + " " + req[3].responseText);
	return;
    }
    var res = eval('(' + req[3].responseText + ')');
    TweetManager.instance.favorites = res;
}

function onNewInbox() {
    if (req[4].status != 200) {
	console.log(req[4].status + " " + req[4].responseText);
	return;
    }
    var res = eval('(' + req[4].responseText + ')');
    TweetManager.instance.inbox = res;
}

function onNewSent() {
    if (req[5].status != 200) {
	console.log(req[5].status + " " + req[5].responseText);
	return;
    }
    var res = eval('(' + req[5].responseText + ')');
    TweetManager.instance.sent = res;
}

TweetManager.instance = new TweetManager();

function main() {
    console.log("starting main()");
    getCurrentUserInfo();
    console.log("before init, read min_id: " + TweetManager.instance.home_read_minid, TweetManager.instance.mentions_read_minid);
    if (localStorage.home_read_minid != null) {
	TweetManager.instance.home_read_minid = localStorage.home_read_minid;
	console.log("read from localStorage home_read_minid " + TweetManager.instance.home_read_minid);
    }
    if (localStorage.mentions_read_minid != null) {
	TweetManager.instance.mentions_read_minid = localStorage.mentions_read_minid;
	console.log("read from localStorage mentions_read_minid" + TweetManager.instance.mentions_read_minid);
    }
    if (TweetManager.instance.home_read_minid == null) {
	TweetManager.instance.home_read_minid = MAXID;
	console.log("home_read_minid set " + MAXID + " " + TweetManager.instance.home_read_minid);
    }
    if (TweetManager.instance.mentions_read_minid == null) {
	TweetManager.instance.mentions_read_minid = MAXID;
	console.log("read_minid[1] set " + MAXID + " " +  TweetManager.instance.mentions_read_minid);
    }
    console.log("now read min_id: " + TweetManager.instance.home_read_minid, TweetManager.instance.mentions_read_minid);

    var i;
    for (i = 0; i < 6; i++) {
	updateNewTwit(i);
	setInterval(updateNewTwit, 60 * 1000, i);
    }
}
}
